import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;

public class driver {
	private static FileWriter writer;
	private static String ERROR = "error";
	private static FileSystem fileSystem = new FileSystem();
	
	enum COMMAND_TYPE {
		CREATE, DELETE, OPEN, CLOSE, READ, WRITE, SEEK, DIRECTORY, INIT, SAVE
	};
	
	public static void main(String[] args) {
		if (args.length == 2) {
			readAndExecuteInput(args);
		}
	}
	
	private static void readAndExecuteInput(String[] args) {
		File inputFile = new File(args[0]);
		File outputFile = new File(args[1]);
		
		try {
			writer = new FileWriter(outputFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		readInput(inputFile);
	}
	
	private static void readInput(File inputFile) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(inputFile));
			String userInput;
			while ((userInput = reader.readLine()) != null) {
				executeInput(userInput);
			}
			reader.close();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void executeInput(String userInput) {
		if (userInput.trim().equals("")) {
			writeOutput("");
			return;
		}
		String[] input = userInput.split(" ");
		
		switch (determineCommandType(input[0])) {
			case CREATE: {
				if (input.length == 2) {
					if (input[1].length() < 4) {
						writeOutput(fileSystem.createFile(input[1]));
					} else {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
				
			case DELETE: {
				if (input.length == 2) {
					if (input[1].length() < 4) {
						writeOutput(fileSystem.deleteFile(input[1]));
					} else {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case OPEN: {
				if (input.length == 2) {
					if (input[1].length() < 4) {
						int index = fileSystem.openFile(input[1]);
						if (index != -1) {
							writeOutput(input[1] + " opened " +
									index);
						} else {
							writeOutput(ERROR);
						}
					} else {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case CLOSE: {
				if (input.length == 2) {
					try {
						int index = Integer.parseInt(input[1]);
						writeOutput(fileSystem.closeFile(index));
					} catch (NumberFormatException e) {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case READ: {
				if (input.length == 3) {
					try {
						int index = Integer.parseInt(input[1]);
						int count = Integer.parseInt(input[2]);
						writeOutput(fileSystem.readFile(index, count));
					} catch (NumberFormatException e) {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case WRITE: {
				if (input.length == 4) {
					try {
						int index = Integer.parseInt(input[1]);
						char data = input[2].charAt(0);
						int count = Integer.parseInt(input[3]);
						writeOutput(fileSystem.writeToFile(index, data, count));
					} catch (NumberFormatException e) {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case SEEK: {
				if (input.length == 3) {
					try {
						int index = Integer.parseInt(input[1]);
						int pos = Integer.parseInt(input[2]);
						writeOutput(fileSystem.seek(index, pos));
					} catch (NumberFormatException e) {
						writeOutput(ERROR);
					}
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case DIRECTORY: {
				if (input.length == 1) {
					writeOutput(fileSystem.directory());
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case INIT: {
				if (input.length == 1) {
					writeOutput(fileSystem.init(null));
				} else if (input.length == 2) {
					writeOutput(fileSystem.init(input[1]));
				} else {
					writeOutput(ERROR);
				}
				break;
			}
			
			case SAVE: {
				if (input.length == 2) {
					writeOutput(fileSystem.save(input[1]));
				} else {
					writeOutput(ERROR);
				}
				break;
			}
		}
	}
	
	private static void writeOutput(String output) {
		try {
			writer.write(output+"\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static COMMAND_TYPE determineCommandType(String command) {
		COMMAND_TYPE commandType = null;
		
		if (command.equalsIgnoreCase("cr")) {
			commandType = COMMAND_TYPE.CREATE;
		} else if (command.equalsIgnoreCase("de")) {
			commandType = COMMAND_TYPE.DELETE;
		} else if (command.equalsIgnoreCase("op")) {
			commandType = COMMAND_TYPE.OPEN;
		} else if (command.equalsIgnoreCase("cl")) {
			commandType = COMMAND_TYPE.CLOSE;
		} else if (command.equalsIgnoreCase("rd")) {
			commandType = COMMAND_TYPE.READ;
		} else if (command.equalsIgnoreCase("wr")) {
			commandType = COMMAND_TYPE.WRITE;
		} else if (command.equalsIgnoreCase("sk")) {
			commandType = COMMAND_TYPE.SEEK;
		} else if (command.equalsIgnoreCase("dr")) {
			commandType = COMMAND_TYPE.DIRECTORY;
		} else if (command.equalsIgnoreCase("in")) {
			commandType = COMMAND_TYPE.INIT;
		} else if (command.equalsIgnoreCase("sv")) {
			commandType = COMMAND_TYPE.SAVE;
		}
		
		return commandType;
	}

}
